    
	Array.prototype.delRepeat = function () {
        this.sort();//排序
        var n = [this[0]];
        for (var i = 1; i < this.length; i++) {
            if (this[i] !== n[n.length - 1]) {
                n.push(this[i]);
            }
        }
        return n;
    }
	Array.prototype.remove = function(val) {  
		var index = this.indexOf(val);  
		if (index > -1) {  
			this.splice(index, 1);  
		}  
	};  
	BUI.use(['bui/calendar','bui/overlay','bui/form','bui/list','bui/data','bui/toolbar'],function(Calendar,Overlay,Form,List,Data,Toolbar){
		  var tpl = [
			  '<li class="bui-list-item">',
				'{name} <span class="pull-right">（{start_time}至{end_time}）</span>',
			  '</li>'
			],
			store = new Data.Store({ //list 和 paging共同使用store,监听store的加载，以及分页信息
			  url : '/?m=umpadmin&c=Eventcfg&a=get_cats',
			  pageSize : 10
			}),
			list = new List.SimpleList({
				render:'#list1',
				elCls:'bui-select-list clearfix',
				store : store,
				itemTpl:tpl.join('')
			}),
			paging = new Toolbar.NumberPagingBar({ //创建数字分页控件
			  render : '#paging',
			  elCls : 'pagination centered',
			  store : store //共同使用store
			});
	 
		  list.render();
		  paging.render();
		  store.load();
       list.on('itemclick', function(ev){
        alert(ev.item.id + '：' + ev.item.name);
      });
	  
		var j_form = new Form.Form({
			srcNode : '#J_Form'
        });
		j_form.render();
		var datepicker = new Calendar.DatePicker({
			trigger:'.calendar',
			showTime:true,
			autoRender : true
		  });
        var dialog = new Overlay.Dialog({
            title:'添加前置条件',
            width:450,
            height:220,
            mask:false,
            buttons:[
              {
                text:'添加',
                elCls : 'button button-primary',
                handler : function(){
                  var addtp = $(".advance").val();
				  var val=$("."+addtp+"-val").val();
				  if(addtp=="time-set"){
					  val = $("."+addtp+"-start-val").val();
					  var val_end = $("."+addtp+"-end-val").val();
				  }
				  var html="";
				  switch(addtp){
					  case "vip-user":
						html="<li class=\"vip-user-set\"><input type=\"hidden\" name=\"vip_user\" value=\""+val+"\"/>3个月内成交"+val+"笔订单<a href=\"#\" data-tp=\"vip-user\" class=\"x-icon x-icon-hover x-icon-mini js-remove\">×</a></li>";
					  break;
					  case "sum-pay":
						html="<li class=\"sum-pay-set\"><input type=\"hidden\" name=\"sum_pay\" value=\""+val+"\"/>3个月内累计消费"+val+"元<a href=\"#\" data-tp=\"sum-pay\" class=\"x-icon x-icon-hover x-icon-mini js-remove\">×</a></li>";
					  break;
					  case "time-set":
						var val_1 = ($(".time-set-end-val").val());
						html="<li class=\"time-set-set\"><input type=\"hidden\" name=\"pay_time_start\" value=\""+val+"\"/><input type=\"hidden\" name=\"pay_time_end\" value=\""+val_1+"\"/>从"+val+"到"+val_1+"之间有订单<a href=\"#\" data-tp=\"time-set\" class=\"x-icon x-icon-hover x-icon-mini js-remove\">×</a></li>";
					  break;
					  case "item-set":
						html="<li class=\"item-set-set\"><input type=\"hidden\" name=\"item_iids\" value=\""+val+"\"/>订单中包含"+val.split(',').length+"商品<a href=\"#\" data-tp=\"item-set\" class=\"x-icon x-icon-hover x-icon-mini js-remove\">×</a></li>";
					  break;
					  case "once-pay":
						html="<li class=\"once-pay-set\"><input type=\"hidden\" name=\"once_pay\" value=\""+val+"\"/>单笔订单达到"+val+"元<a href=\"#\" data-tp=\"once-pay\" class=\"x-icon x-icon-hover x-icon-mini js-remove\">×</a></li>";
					  break;
				  }
				  $("."+addtp+"-set").remove();
				  $(html).prependTo(".adv-set");
				  $(".js-remove").on("click",function(){
					  var tp = $(this).attr("data-tp");
					  $("."+tp+"-set").remove();
				  });
                  this.close();
                }
              },{
                text:'关闭',
                elCls : 'button',
                handler : function(){
                  this.close();
                }
              }
            ],
            
            contentId:"rules"
          });
	var loading="<div class=\"loading\" style=\"width:34px;height:34px;margin:100px auto;background:url()\"></div>";
	var itemdialog = new Overlay.Dialog({
		title:'选择商品',
		width:990,
		height:600,
		mask:false,
		content:"<div class=\"load\" ></div>"
	});
	var q="";
	var item_iids=new Array();
	$(".js-load-items").on("click",function(){
		itemdialog.show();
		itemdialog.set("y",50);
		$(loading).appendTo(".load");
		var item_value = $(".item-set-val").val();
		if(item_value){
			item_iids = item_value.split(',');
		}
		$.ajax(
		{"url":"/?m=umpadmin&a=load_items",	
			"data":{"q":q,"pg":$(".bui-pb-page").val()},
			"dataType": "json",
			"success":function(data,status){
				var isfirst=(data.pg==1?true:false);
				var islast =(data.pg==data.sumpages?true:false);
				var items = "";
				    items +="<div class=\"bui-grid \" style=\"margin:10px 50px;clear:both\" >";
					items +='<ul class="bui-pagingbar bui-bar pull-left" role="toolbar" id="bar3" aria-disabled="false" aria-pressed="false"><li class="bui-bar-item-button bui-bar-item bui-pb-first bui-inline-block '+(isfirst?'bui-bar-item-button-disabled bui-bar-item-disabled':'')+'" aria-disabled="true" id="first"><button type="button" '+(isfirst?'class="bui-button-disabled" disabled="disabled"':'')+'>首 页</button></li><li class="bui-bar-item-button bui-bar-item bui-pb-prev bui-inline-block '+(isfirst?'bui-bar-item-button-disabled bui-bar-item-disabled':'')+'" aria-disabled="true" id="prev"><button type="button" '+(isfirst?'class="bui-button-disabled" disabled="disabled"':"")+'>上一页</button></li><li class="bui-bar-item-separator bui-bar-item bui-inline-block" aria-disabled="false" id="bar-item-separator1" role="separator"></li><li class="bui-bar-item-text bui-bar-item bui-inline-block" aria-disabled="false" id="totalPage" aria-pressed="false">共 '+data.sumpages+' 页</li><li class="bui-bar-item-text bui-bar-item bui-inline-block" aria-disabled="false" id="curPage">第 <input type="text" autocomplete="off" class="bui-pb-page" size="20" value="'+data.pg+'" name="inputItem"> 页</li><li class="bui-bar-item-button bui-bar-item bui-pb-skip bui-inline-block" aria-disabled="false" id="skip"><button type="button" class="">确定</button></li><li class="bui-bar-item-separator bui-bar-item bui-inline-block '+(isfirst?'bui-bar-item-button-disabled bui-bar-item-disabled':'')+'" aria-disabled="false" id="bar-item-separator2" role="separator"></li><li class="bui-bar-item-button bui-bar-item bui-pb-next bui-inline-block '+(isfirst?'bui-bar-item-button-disabled bui-bar-item-disabled':'')+'" aria-disabled="false" id="next"><button type="button" '+(islast?'class="bui-button-disabled" disabled="disabled"':'')+'>下一页</button></li><li class="bui-bar-item-button bui-bar-item bui-pb-last bui-inline-block '+(islast?'bui-bar-item-button-disabled bui-bar-item-disabled':'')+'" aria-disabled="false" id="last"><button type="button" '+(islast?'class="bui-button-disabled" disabled="disabled"':'')+'>末 页</button></li><li class="bui-bar-item-separator bui-bar-item bui-inline-block" aria-disabled="false" id="bar-item-separator3" role="separator"></li><li class="bui-bar-item-text bui-bar-item bui-inline-block" aria-disabled="false" id="totalCount">共'+data.total_results+'条记录</li></ul>';
					items +="<i><input type=\"text\" class=\"input-small q \" value=\""+q+"\" /><button class=\"button button-mini js-search\">搜索</button></i></div>";
					items +="<ul class=\"clearfix \" style=\"height:600px;padding:0 0 10px 40px;overflow-y:scroll;\">";
				for(var i=0;i<data.items.item.length;i++){
					var btncls = item_iids.indexOf(data.items.item[i].num_iid.toString())>=0?"button-danger":"button-primary";
					items+='<li class="item item'+data.items.item[i].num_iid+'">';
					items+='<input type="hidden" name="price[]" value="'+data.items.item[i].price+'" />';
					items+='<input type="hidden" name="item_iid[]" value="'+data.items.item[i].num_iid+'" />';
					items+='<input type="hidden" name="item_pic_url[]" value="'+data.items.item[i].pic_url+'" />';
					items+='<input type="hidden" name="item_title[]" value="'+data.items.item[i].title+'" />';
					items+='<dl><img src="'+data.items.item[i].pic_url+'_120x120"  /></dl>';
					items+='<ol>'+data.items.item[i].title+'</ol>';
					items+='<ol><button class="button buttom-mini '+btncls+' js-select" data-iid="'+data.items.item[i].num_iid+'" type=\"button\">选择</button></ol>';
					items+='</li>';
				}
				items+="</ul>";
				items+='<div class="centered" style="margin-bottom:10px"><button class=\"button button-danger js-close \">确定</button></div>';
				$(".load").empty();
				$(".loading").remove();
				$(items).appendTo(".load");
				$(".js-select").on("click",function(){
					var curiid = $(this).attr("data-iid");
					if(item_iids.length>50){
						alert("每个活动最多选择50个商品！");
						return;
					}
					if(item_iids.indexOf(curiid)>=0){						
						$(this).removeClass("button-danger");
						$(this).addClass("button-primary");
						item_iids.remove(curiid);
						step2_items(curiid,"remove");
					}else{
						$(this).removeClass("button-primary");
						$(this).addClass("button-danger");
						item_iids.push(curiid);
						item_iids = item_iids.delRepeat();
						step2_items(curiid,"add");
					}
					$(".item-set-val").val(item_iids.join(","));
					$(".item-num").text("已选择"+item_iids.length+"个商品");
				});
				$(".js-search").on("click",function(){
					q = $(".q").val();
					$(".js-load-items").click();
				});
				$(".bui-pb-next").on("click",function(){
					var curpg = $(".bui-pb-page").val();
					var pg = curpg<data.sumpages?curpg++:data.sumpages;
					$(".bui-pb-page").val(curpg<data.sumpages?curpg++:data.sumpages);
					$(".js-load-items").click();
				});
				$(".bui-pb-last").on("click",function(){
					$(".bui-pb-page").val(data.sumpages);
					$(".js-load-items").click();
				});
				$(".bui-pb-prev").on("click",function(){
					var curpg = $(".bui-pb-page").val();
					$(".bui-pb-page").val(curpg>1?curpg-1:1);
					$(".js-load-items").click();
				});
				$(".bui-pb-first").on("click",function(){
					$(".bui-pb-page").val(1);
					$(".js-load-items").click();
				});
				$(".bui-pb-skip").on("click",function(){
					$(".js-load-items").click();
				});
				$(".js-close").on("click",function(){
					itemdialog.close();
				});
			}
		});
	});
      $('#btnShow').on('click',function () {
        dialog.show();
      });
	$(".js-advance").on("click",function(){
		var cls = $(this).attr("data-advance");
		$(".rule").hide();
		$("."+cls).show();
		$(".advance").val(cls);
	});
	$(".js-remove").on("click",function(){
		  var tp = $(this).attr("data-tp");
		  $("."+tp+"-set").remove();
	});
	function step2_items(item_iid,ac){
		if(ac=="add"){
			var item = $(".item"+item_iid).clone(true);
			item.removeClass("item"+item_iid);
			item.addClass("add_item_"+item_iid);
			item.children("ol").children(".js-select").text("删除");
			$(".js-added").append(item);
		}
		if(ac=="remove"){
			$(".add_item_"+item_iid).remove();
		}
	}
	$(".js-rec_text").on("click",function(){
		var dialog = new Overlay.Dialog({
			title:'统一填写',
			width:500,
			height:200,
			mask:false,
			bodyContent:"<textarea class=\"all_recom\"></textarea>",
			buttons:[
              {
                text:'添加',
                elCls : 'button button-primary',
                handler : function(){
					$(".recommend").val($(".all_recom").val());
                  this.close();
				}
			}]
		});
		dialog.show();
	});
	
	$(".js-msg").on("click",function(){
		if(!j_form.isValid()){
			var msg = '<div class="centered"><span class="x-field-error"><span class="x-icon x-icon-mini x-icon-error">!</span><label class="x-field-error-text">填写的内容出错，请检查字段！</label></span></div>';
		}else{
			var msg = '<div class="centered">系统正在处理您的请求,请勿刷新浏览器！</div>';
			msg+="<div style='margin:0 auto;height:60px;width:100px;background:url(http://img04.taobaocdn.com/tps/i4/T1zNxYFa8bXXcUNunv-150-100.gif) -20px'></div>"
		}
		var dialog = new Overlay.Dialog({
			"title":'系统正在处理您的请求',
			"width":500,
			"height":200,
			"mask":true,
			"bodyContent":msg,
			"buttons":[]
		});
		dialog.show();
	});
});